package gui.catalogues.telephones.models;

import gui.commons.ToolbarListener;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.Timer;

public class TelephoneToolbar extends JToolBar implements ActionListener {

	/*
	 * Variables declaration
	 */
	private JButton jbSave, jbAdd, x;
	private ToolbarListener tl;
	private int iconBarWidth = 22, iconBarHeight = 22, aMillion = 1000000;
	
//	private Timer timer;
//	private long iTime, fTime;

	/*
	 * Image resizing
	 */
	private File imgFile;
	private Image img, resizedImg;
	private URL url;
	private String strUrl;

	public TelephoneToolbar() {

		/*
		 * Initializing variables
		 */
		// x = new JButton("");
		jbSave = new JButton();

		// setting an icon to it
		jbSave.setIcon(createIcon("../../../../images/Save_22x22.png", "Guardar matriz"));
//		save.setIcon(createResizedIcon("../images/Save.png", "Guardar matriz",
//				iconBarWidth, iconBarHeight));
		jbSave.setToolTipText("Guardar.");

		jbAdd = new JButton();
		jbAdd.setToolTipText("Agregar.");
		// setting an icon to it
		jbAdd.setIcon(createIcon("../../../../images/Add_22x22.png", "Agregar"));

		// set the border, but the toolbar will not be undockable
		setBorder(BorderFactory.createEtchedBorder());
		// still, if you don't want a border, this is the setting for the
		// toolbar to NOT be undockable as well
		setFloatable(false);

		/*
		 * ActionListeners
		 */
		jbAdd.addActionListener(this);
		jbSave.addActionListener(this);

		/*
		 * Adding components to the toolbar
		 */
		add(jbAdd);
		add(jbSave);
	}

	private ImageIcon createIcon(String path, String desc) {
//		iTime = System.nanoTime()/aMillion;
		url = getClass().getResource(path);
		if (url == null)  System.err.println("Unable to load image: " + path);
//		ImageIcon ii = new ImageIcon(url, desc);
//		fTime = System.nanoTime()/aMillion;
//		System.out.println("createIcon - iTime: " + iTime + ", fTime: " + fTime);
//		System.out.println("time taken to create icon '" + path + "': " + (fTime - iTime));
//		return ii;
		return new ImageIcon(url, desc);
	}

	private ImageIcon createResizedIcon(String path, String desc, int width,
			int height) {
//		iTime = System.nanoTime()/aMillion;
		try {
			url = getClass().getResource(path);
			strUrl = url.toURI().getPath();
			// System.out.println("url: " + url + ", toUri: " +
			// url.toURI().getPath());
		} catch (URISyntaxException e) {
			System.out.println("URISyntaxException: " + e);
		}
		imgFile = new File(strUrl);
		try {
			img = ImageIO.read(imgFile);
		} catch (IOException e) {
			System.out.println("IOException@createResizedIcon: " + e);
		}
		resizedImg = img.getScaledInstance(width, height, Image.SCALE_FAST);
//		ImageIcon ii = new ImageIcon(resizedImg);
//		fTime = System.nanoTime()/aMillion;
//		System.out.println("createResizedIcon - iTime: " + iTime + ", fTime: " + fTime);
//		System.out.println("time taken to create a resized icon '" + path + "': " + (fTime - iTime));
//		return ii;
		return new ImageIcon(resizedImg);
	}

	// method to assign a textPanel to the Toolbar so the latter will be
	// listened
	public void setToolbarListener(ToolbarListener tl) {
		this.tl = tl;
	}

	public void actionPerformed(ActionEvent e) {
		// emit the text gotten from the -casted into JButton- event source.
		// sl.textEmitted(((JButton)e.getSource()).getText() + "\n");
		if ((JButton) e.getSource() == jbAdd) {
			tl.addItemEventOccured();
		} else if ((JButton) e.getSource() == jbSave) {
			tl.saveItemEventOccured();
		}
	}
}
